package com.wlanauth.web.action;

import java.util.Date;

import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.criterion.Restrictions;

import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.domain.manage.Message;
import com.wlanauth.domain.manage.User;
import com.wlanauth.domain.radius.RadAcct;
import com.wlanauth.service.manage.MessageService;
import com.wlanauth.web.core.BaseActionSupport;
import com.wlanauth.web.core.JsonResult;
import com.wlanauth.web.core.SystemConstants;
/**
 * 留言
 * @author zz
 * @date 2012-5-23
 * @email charles.zz.life@gmail.com
 */
public class MessageAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;
	/**
	 * 消息实体
	 */
	private Message entity;
	/**
	 * 消息服务
	 */
	private MessageService messageService;
	/**
	 * 验证码
	 */
	private String vlidateCode;
	/**
	 * 保存
	 * @return
	 */
	public String save(){
		User u = this.getCurrentUser();
		if(u==null){//需要验证码
			if(StringUtils.isBlank(vlidateCode)){
				this.jsonResult = new JsonResult(this,"请输入验证码",false);
				return this.actionJsonResult();
			}
			Object code = this.request.getSession().getAttribute(SystemConstants.CODE_VALIDATE_MESSAGE);
			if(!this.vlidateCode.toLowerCase().equals(code==null?"":code.toString().toLowerCase())){
				this.jsonResult = new JsonResult(this,"验证码不正确",false);
				return this.actionJsonResult();
			}
		}
		this.entity.setChecked(false);
		this.entity.setCreateTime(new Date());
		this.entity.setIp(this.request.getRemoteAddr());
		messageService.save(entity);
		this.jsonResult = new JsonResult(this,true);
		return this.actionJsonResult();
	}
	public String viewDetailPage(){
		
		this.entity = messageService.get(this.entity.getId());
		return "viewDetailPage";
	}
	public String list(){
		GeneralCriteria criteria = this.buildWebQueryCriteria(Message.class);
		if(entity!=null){
			if(entity.getUsername()!=null){
				criteria.add(Restrictions.eq("username", entity.getUsername()));
			}
			if(entity.getChecked()!=null){
				criteria.add(Restrictions.eq("checked", entity.getChecked()));
			}
		}
		GeneralCriteriaResult<RadAcct> result = criteria.executeQuery();
		this.jsonResult = new JsonResult(this,result.getData(),result.getTotal());
		return this.actionJsonResult();
	}
	public Message getEntity() {
		return entity;
	}
	public void setEntity(Message entity) {
		this.entity = entity;
	}
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	public void setVlidateCode(String vlidateCode) {
		this.vlidateCode = vlidateCode;
	}
	
	
}
